package com.betterjr.modules.risk.loan.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.modules.risk.loan.dao.LoanTransactionMapper;
import com.betterjr.modules.risk.loan.data.LoanProjectRelationConstantCollections;
import com.betterjr.modules.risk.loan.entity.LoanTransaction;
import com.google.common.collect.Lists;

@Service
public class LoanTransactionService extends BaseService<LoanTransactionMapper, LoanTransaction> {

    /**
     * 
     * @Title: saveOrUpdateLoanTransaction 
     * @Description:新增或者修改交易数据
     * @param anLoanTransaction
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年7月4日 上午9:44:31
     */
    public LoanTransaction saveOrUpdateLoanTransaction(LoanTransaction anLoanTransaction) {

        BTAssert.notNull(anLoanTransaction, "交易数据为空");

        BTAssert.isTrue(StringUtils.isNoneBlank(anLoanTransaction.getId()), "交易数据为空");

        checkTransaction(anLoanTransaction);

        if (this.selectByPrimaryKey(anLoanTransaction.getId()) == null) {
            this.insertSelective(anLoanTransaction);
        } else {
            this.updateByPrimaryKeySelective(anLoanTransaction);
        }

        return anLoanTransaction;

    }

    /**
     * 
     * @Title: queryLoanTransaction 
     * @Description: 查询交易数据 
     * @param ids
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年7月4日 上午9:50:29
     */
    public List<LoanTransaction> queryLoanTransaction(String ids) {

        if (StringUtils.isNoneBlank(ids)) {

            Map map = QueryTermBuilder.newInstance()
                    .put("id", ids.split(LoanProjectRelationConstantCollections.FILE_DATA_SEPATORY_CHAR)).build();

            return this.selectByProperty(map, "id Desc");

        }

        return Lists.newArrayList();

    }

    /**
     * 
     * @Title: checkTransaction 
     * @Description: 校验交易数据) 
     * @param anLoanTransaction TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年7月4日 上午9:45:19
     */
    private void checkTransaction(LoanTransaction anLoanTransaction) {

        BTAssert.isTrue(anLoanTransaction.getBalance() != null, "交易数据的金额不能为空");

    }

}
